package com.dtp.logging;

import com.dtp.logging.log4j2.DtpLog4j2Logging;
import com.dtp.logging.logback.DtpLogbackLogging;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * DtpLogging初始化器
 *
 * @author yanhom
 * @since 1.0.5
 *
 * @remark muse
 **/
@Slf4j
public class DtpLoggingInitializer {
    private static AbstractDtpLogging dtpLogging;

    /** 初始化dtpLogging日志 */
    static {
        try {
            Class.forName("ch.qos.logback.classic.Logger");
            dtpLogging = new DtpLogbackLogging(); // 默认实现类是DtpLogbackLogging
        } catch (ClassNotFoundException e) {
            try {
                Class.forName("org.apache.logging.log4j.LogManager");
                dtpLogging = new DtpLog4j2Logging(); // 如果发生异常，则初始化DtpLog4j2Logging
            } catch (ClassNotFoundException classNotFoundException) {
                log.error("DynamicTp initialize logging failed, please check whether logback or log4j related dependencies exist.");
            }
        }
    }

    /** 加載配置信息 */
    public void loadConfiguration() {
        if (Objects.isNull(dtpLogging)) return;

        dtpLogging.loadConfiguration(); // 加载日志配置信息
        dtpLogging.initMonitorLogger(); // 初始化LogHelper中的monitorLogger变量
    }


    /** 单例模式实现 */
    private static class LoggingInstance {
        private static final DtpLoggingInitializer INSTANCE = new DtpLoggingInitializer();
    }
    public static DtpLoggingInitializer getInstance() {return LoggingInstance.INSTANCE;}
}
